const userService = require('../services/userService')
const RoleModel = require('../models/RoleModel')
const jwt = require('../util/jwt')
const cryptPwd = require('../util/cryptPwd')

const userController = {
  async register(req, res, next) {
    const { username, password } = req.body

    let roleList = await RoleModel.find()
    let personId = roleList.find(item => item.name === '普通用户')._id
    let adminId = roleList.find(item => item.name === '管理员')._id

    let data = { username, password: cryptPwd(password) }
    if (username == 'admin') {
      data.role = adminId
    } else {
      data.role = personId
    }
    try {
      await userService.register(data)
      res.send({
        code: 200,
        msg: '注册成功',
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async login(req, res, next) {
    const { username, password } = req.body
    let roleList = await RoleModel.find()
    let adminId = roleList.find(item => item.name === '管理员')._id
    try {
      let data = await userService.login({ username, password: cryptPwd(password) })
      if (req.query.admin && String(data.role) != adminId) {
        return res.status(401).send({
          code: 401,
          msg: '需要管理员权限才能访问',
        })
      }
      const token = jwt.generate(
        {
          id: data._id,
          username: data.username,
          role: data.role?.name,
        },
        '1 days'
      )
      res.header('Authorization', token)
      res.send({
        code: 200,
        msg: 'ok',
        token,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: '用户名或密码错误',
      })
    }
  },
  async getUserInfo(req, res, next) {
    let data = await userService.getUserInfo(req.auth.id)
    res.send({
      code: 200,
      msg: 'ok',
      data,
    })
  },
  async updateUserInfo(req, res, next) {
    try {
      let body = {
        ...req.body,
      }
      if (body.password) body.password = cryptPwd(req.body.password)
      let data = await userService.updateUserInfo(body)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getAllUser(req, res, next) {
    try {
      let data = await userService.getAllUser()
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getAllRole(req, res, next) {
    try {
      let data = await userService.getAllRole()
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async deleteUser(req, res, next) {
    try {
      let data = await userService.deleteUser(req.body.id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
}

module.exports = userController
